﻿using System;
using System.Linq;
using System.Text;
using GONES.IBLL;
using GONES.IDAL;
using GONES.Model;
using log4net;
using System.Reflection;
using GONES.Tools.Common;
using Newtonsoft.Json;
using System.Collections.Generic;

namespace GONES.BLL
{
    public class S_DepartManager : IS_DepartManager
    {
        public IS_DepartDao S_DepartDao;
        public S_LogManager S_LogManager;
        protected static readonly ILog Log =
               LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        public IS_LogDao S_LogDao;
        private readonly IUserInfo _userInfo;

        public string ErrMsg { get; set; }

        public S_DepartManager(IUserInfo userinfo)
        {
            _userInfo = userinfo;
        }

        public S_Depart Get(string id)
        {
            return S_DepartDao.Get(id);
        }

        public void Add(S_Depart depart)
        {
            if (IsError(depart))
                return;
            depart.CreateTime = System.DateTime.Now;
            S_DepartDao.Save(depart);
            //savalog
            SaveLog("部门管理", "部门添加", "系统管理", depart);
        }

       

        public void Edit(S_Depart depart)
        {
            if (IsError(depart))
                return;
            depart.ModifyTime = System.DateTime.Now;
            S_DepartDao.Update(depart);
           //savalog
            SaveLog("部门管理", "部门修改", "系统管理", depart);
        }

        public void Delete(string id)
        {
            S_Depart depart = S_DepartDao.Get(id);
            S_DepartDao.Delete(depart);
            //savalog
            SaveLog("部门管理", "部门删除", "系统管理", depart);
        }

        /// <summary>
        /// 获取指定部门下面的子部门个数
        /// </summary>
        /// <param name="parentid"></param>
        /// <returns>部门数</returns>
        public int GetOrgCountByParentId(string parentid)
        {
            return S_DepartDao.GetHqlList(string.Format("select e from S_Depart e where 1=1 and e. ParentId='{0}'",parentid)).Count;
        }

        /// <summary>
        /// 获取指定部门下的雇员数量
        /// </summary>
        /// <param name="orgid">部门ID</param>
        /// <returns>雇员数</returns>
        public int GetEmpCountByOrgId(string orgid)
        {
            return S_DepartDao.GetHqlList(string.Format("select e from S_Employees e where 1=1 and e.DepartId='{0}'", orgid)).Count;
        }

        public IList<S_Depart> List()
        {
            return S_DepartDao.GetHqlList("select e from S_Depart e where 1=1   order by e.ParentId ");
        }

        public IList<S_Depart> List(IList<DataFilter> filters)
        {
            string strFilter = DataOperate.GetHqlstrByExtFilter(filters, "e");
            string hql = "SELECT e FROM S_Depart e  where  1=1 " +
                                       (string.IsNullOrEmpty(strFilter) ? string.Empty : " and " + strFilter) +
                                       " ORDER BY e.ID DESC";

            return S_DepartDao.GetHqlList(hql);
        }

        private void SaveLog(string module, string operate, string logtype, S_Depart depart)
        {
            S_LogManager = new S_LogManager(_userInfo);
            string detail = string.Format("{0}:部门名称是{1}", operate, depart.DepartName);
            SaveLog(module, operate, logtype, detail);
            if (Log.IsInfoEnabled)
                Log.Info(string.Format("{0}：{1}", operate, JsonConvert.SerializeObject(depart)));
        }

        protected bool IsError(S_Depart depart)
        {
            if (string.IsNullOrEmpty(depart.DepartName))
            {
                ErrMsg = "部门名称不能为空!";
            }
            else if (S_DepartDao.GetList().Where(g => g.DepartName == depart.DepartName).Count() > 0)
            {
                ErrMsg = "该部门名称已经存在！";
            }
            if (!string.IsNullOrEmpty(ErrMsg))
                return true;
            return false;
        }

        /// <summary>
        /// 保存日志
        /// </summary>
        /// <param name="module">模块名称</param>
        /// <param name="operate">操作名称</param>
        /// <param name="logtype">日志类型（系统管理、基础资料等）</param>
        /// <param name="detail">详细日志</param>
        protected void SaveLog(string module, string operate, string logtype, string detail)
        {
            try
            {
                S_LogDao.Save(new S_Log
                {
                    ID = Rnd.Guid.ToString(),
                    Module = module,
                    Operate = operate,
                    Detail = detail,
                    LogType = logtype,
                    OpDate = DateTime.Now,
                    CreateUser = _userInfo.CLoginInfo.UserName,
                    CreateTime = DateTime.Now,
                    ModifyUser = _userInfo.CLoginInfo.UserName,
                    ModifyTime = DateTime.Now,
                    Ip = Tools.Common.Tools.GetIP()
                });
            }
            catch
            {
            }
        }
    }
}
